//
//  AppDelegate+YWLaunchGuideView.m
//  YWLaunchGuideView
//
//  Created by NeiQuan on 16/6/13.
//  Copyright © 2016年 NeiQuan. All rights reserved.
//
#define YWidth [UIScreen  mainScreen].bounds.size.width
#define YWHeight [UIScreen  mainScreen].bounds.size.height
#import "AppDelegate+YWLaunchGuideView.h"
#include <objc/runtime.h>
static  const  void *YWlanuchkey= &YWlanuchkey;
@implementation AppDelegate (YWLaunchGuideView)
-(void)YWmakeScrollViewLaunchGuide:(NSArray *)ScollViewArray{
    
       UIScrollView *YWScrollView=[[ UIScrollView alloc] initWithFrame:self.window.frame];
    
          for (NSInteger YWindex=0; YWindex<ScollViewArray.count; YWindex++)
          {
              UIImageView *YWimageView=[[ UIImageView alloc] initWithFrame:CGRectMake(YWidth*YWindex, 0, YWidth, YWHeight)];
//#warning 注意数组里面必须是本地图片名称，目前网络的还没有时间去写
              [YWimageView setImage:[ UIImage imageNamed:ScollViewArray[YWindex]]];
              [YWScrollView addSubview:YWimageView];//添加子对象
              if (YWindex==ScollViewArray.count-1) {
                 __weak typeof(self)weakself =self;//防止死循环
                  UITapGestureRecognizer *tap=[[ UITapGestureRecognizer alloc] initWithTarget:weakself action:@selector(YWScrollViewTapClick)];
                  YWimageView.userInteractionEnabled=YES;//开启图片的交互
                  [YWimageView addGestureRecognizer:tap];
              }
         }
      YWScrollView.showsVerticalScrollIndicator=NO;
      YWScrollView.showsHorizontalScrollIndicator=NO;
    YWScrollView.pagingEnabled =YES;
      YWScrollView.contentSize=CGSizeMake(YWidth*ScollViewArray.count, 0) ;
    YWScrollView.tag=500000;
      [self.window addSubview:YWScrollView];
 }
//最后一张图点击进入app
//需要记录当前为
-(void)YWScrollViewTapClick{
    UIScrollView *view=(UIScrollView*)[self.window viewWithTag:500000];
    [view removeFromSuperview];//从窗口移除，以免出现bug
    
}
/**
 *  根据图片名称设置类视于天猫启动图的效果图
 *
 *  @param ImageName <#ImageName description#>
 */
-(void)YWmakeSplashViewGuide:(NSString *)ImageName{
    //启动图片动画
    UIImageView *image=[[ UIImageView alloc] initWithFrame:[ UIScreen mainScreen].bounds];
    [image setImage:[ UIImage imageNamed:[ self getCurrentDevice]]];
    [self.window addSubview:image];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.5 animations:^{
            image.alpha = 0.0;
            image.transform  = CGAffineTransformMakeScale(2, 2);
        } completion:^(BOOL finished) {
            [image removeFromSuperview];
          
        }];
    });
}
-(NSString*)getCurrentDevice{
    NSInteger  YWheight =[ UIScreen mainScreen] .bounds.size.height;
    switch (YWheight*2) {
        case 960:
            return @"640x960";
            break;
        case 1136:
            return @"640x1366";
            break;
        case 1334:
            return @"750x1334";
            break;
        case 2208:
            return @"1242x2208";
            break;
        default:
            break;
    }
    return @"1242x2208";
}
//*****************************************************************
-(void)YWmakeSplashViewTimer:(NSString *)ImageName{
    
    UIImageView *image=[[ UIImageView alloc] initWithFrame:[ UIScreen mainScreen].bounds];
    [image setImage:[ UIImage imageNamed:[ self getCurrentDevice]]];
    image.userInteractionEnabled=YES;
    UIBezierPath *circlepath=[ UIBezierPath  bezierPathWithRoundedRect:CGRectMake(YWidth-80, 30, 60, 60) cornerRadius:30];
    CAShapeLayer *ShapeLayer=[ CAShapeLayer layer];//边缘效果
    ShapeLayer.frame=image.frame;
    ShapeLayer.path=circlepath.CGPath;
    ShapeLayer.fillColor=[ UIColor whiteColor].CGColor;
    ShapeLayer.strokeColor=[ UIColor redColor].CGColor;
    ShapeLayer.strokeStart=0.0;
    ShapeLayer.strokeEnd=0.002;
    ShapeLayer.lineWidth=2.0;
    ShapeLayer.lineCap=@"round";
    [image.layer addSublayer:ShapeLayer];
    
    UILabel *lable=[[ UILabel alloc] initWithFrame:CGRectMake(YWidth-80, 30, 60, 60)];
    lable.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap=[[ UITapGestureRecognizer alloc] initWithTarget:self action:@selector(directEnter)];
    [lable addGestureRecognizer:tap];
    lable.text=@"跳过";
    [lable setTextAlignment:NSTextAlignmentCenter];
    lable.textColor=[ UIColor blackColor];
    [image addSubview:lable];

    NSTimer *timer= [ NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timer) userInfo:nil repeats:YES];
    NSDictionary *launchdic=@{@"YWlanuchlable":lable,@"YWlaunchImage":image,@"YWlaunchLayer":ShapeLayer,@"YWlanuchTimer":timer};
    objc_setAssociatedObject(self, YWlanuchkey, launchdic, OBJC_ASSOCIATION_RETAIN);
    [self.window addSubview:image];
    
}
-(void)directEnter{
    NSDictionary *launchdic=objc_getAssociatedObject(self, YWlanuchkey);
    NSTimer *timer=launchdic[@"YWlanuchTimer"];
    UIImageView *image=launchdic[@"YWlaunchImage"];
    [timer invalidate];
    [image removeFromSuperview];
    return;
}
-(void)timer{
    
    NSDictionary *launchdic=objc_getAssociatedObject(self, YWlanuchkey);
    CAShapeLayer *ShapeLayer=launchdic[@"YWlaunchLayer"];
    NSTimer *timer=launchdic[@"YWlanuchTimer"];
    UIImageView *image=launchdic[@"YWlaunchImage"];
    if ((int)ShapeLayer.strokeEnd ==1)
    {
        [timer invalidate];
        [image removeFromSuperview];
        return;
    }
    [ UIView animateWithDuration:0.01 animations:^{
        
    ShapeLayer.strokeEnd = ShapeLayer.strokeEnd+1/150.0;
    }];
  
}


//**********************倒计时广告启动图效果*******************************************
-(void)YWmakeLanuceViewTimer:(NSString *)ImageName{
    
    UIImageView *image=[[ UIImageView alloc] initWithFrame:[ UIScreen mainScreen].bounds];
    [image setImage:[ UIImage imageNamed:[ self getCurrentDevice]]];
    image.userInteractionEnabled=YES;
    [self.window addSubview:image];
    
    
    UILabel *lable=[[ UILabel alloc] initWithFrame:CGRectMake(YWidth-80, 40, 60, 21)];
    lable.textColor=[ UIColor whiteColor];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.font=[ UIFont systemFontOfSize:12];
    lable.text=@"5秒跳过";
    lable.userInteractionEnabled=YES;
    UITapGestureRecognizer *lableClick=[[ UITapGestureRecognizer alloc] initWithTarget:self action:@selector(YWtimerClick)];
    [lable addGestureRecognizer:lableClick];
    
    
    CALayer *layer=[ CALayer layer];
    layer.frame=lable.bounds;
    layer.backgroundColor=[ UIColor colorWithWhite:0.4 alpha:0.8].CGColor;
    layer.cornerRadius=10;
    layer.masksToBounds=YES;
    [lable.layer addSublayer:layer];
    [image addSubview:lable];
    
    
    NSDictionary *launchdic=@{@"YWlanuchlable":lable,@"YWlaunchImage":image};
    objc_setAssociatedObject(self, YWlanuchkey, launchdic, OBJC_ASSOCIATION_RETAIN);
    
    //定时器
    NSTimeInterval period = 1.0;//每秒执行
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_source_t  timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0);
    __block NSInteger duration =5;
    dispatch_source_set_event_handler(timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
            duration--;
            [lable setText:[ NSString stringWithFormat:@"%li秒跳过",duration]];
            if (duration==0)
            {
                dispatch_source_cancel(timer);
                [self YWtimerClick];
                return ;
            }
            
            
        });
    });
    dispatch_resume(timer);
    
}
-(void)YWtimerClick{
    
    NSDictionary *launchdic=objc_getAssociatedObject(self, YWlanuchkey);
    UIImageView *imageView=launchdic[@"YWlaunchImage"];
    [imageView removeFromSuperview];
    imageView=nil;
    
}




//新浪微博
-(void)YWlaunchSinawithUserName:(NSString *)userName imageName:(NSString *)ImageName{
    
    //UIImageView *headerImageView=[[ UIImageView alloc] initWithFrame:CGRectMake((YWidth-60)*0.5, 100, 60, 60)];
    
    
    
    
}
@end
